package server.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.TransfusionReview;
import server.business.pojo.TransfusionSurgey;
import server.business.service.BloodTestService;
import server.business.service.PatientService;
import server.business.service.TransfusionReviewService;
import server.business.service.TransfusionSurgeyService;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.JsonResult;
import server.business.utils.results.lrz.Result;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;

@RestController
@RequestMapping("/transfusionSurgey")
public class TransfusionSurgeyController {

    @Autowired
    private TransfusionSurgeyService transfusionSurgeyService;
    @Autowired
    private TransfusionReviewService transfusionReviewService;
    @Autowired
    private BloodTestService bloodTestService;
    @Autowired
    private PatientService patientService;

    // *************************************myq*************************start
    /**
     * 展示术中输血信息
     *
     * @return
     */
    @RequestMapping("/normal/list")
    @ResponseBody
    public JsonResult<Object> listPage() {
        Page<TransfusionReview> pageList = transfusionReviewService.getSurgeyInfo();
        return JsonResult.success(pageList);
    }

    /**
     * 术中输血申请提交
     *
     * @param transfusionSurgey
     * @return
     */
    @RequestMapping(value = "/normal/addformdata", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Object> saveSurgeyApplication(@RequestBody @Valid TransfusionSurgey transfusionSurgey,
                                                    TransfusionReview transfusionReview,@RequestParam(name = "applicantId") String applicantId) {

        long transfusionNumberId = Long.parseLong(transfusionSurgey.getTransfusionType());
        GenerateNumberBySnowFlake transfusionNumber1 = new GenerateNumberBySnowFlake(transfusionNumberId, "S");
        transfusionSurgey.setTransfusionNumber(transfusionNumber1.genNumberByflag());
        System.out.println(transfusionNumber1.genNumberByflag());
        transfusionSurgeyService.tranSurgeyAdd(transfusionSurgey);
        String transfusionNumber = transfusionSurgey.getTransfusionNumber();
        transfusionReviewService.insertRecord(transfusionNumber);
        transfusionReview.setState("1");
        System.out.println(transfusionReview.getState());
        transfusionReviewService.updateReview(transfusionNumber, transfusionReview);
        return JsonResult.success();
    }

    /**
     * 输入病历号自动回填身份证号（临床输血申请）
     */
    @RequestMapping(value = "/normal/getIdcard",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<Object> getIdcard(HttpServletRequest httpServletRequest){
        System.out.println(httpServletRequest.getParameter("medicalRecordNumber"));
        String medicalRecordNumber=httpServletRequest.getParameter("medicalRecordNumber");
        System.out.println(patientService.getIdcard(medicalRecordNumber));
        return JsonResult.success( patientService.getIdcard(medicalRecordNumber));
    }

    /**
     * 点击审核展示输血申请单信息(只根据输血申请单号)
     */
    @PostMapping("/normal/applyList")
    @ResponseBody
    public JsonResult<Object> getClinicInfo(HttpServletRequest httpServletRequest) {
        String transfusionNumber = httpServletRequest.getParameter("transfusionNumber");
        System.out.println(transfusionSurgeyService.getSurInfo(transfusionNumber));
        ArrayList<Object> listOne = transfusionSurgeyService.getSurInfo(transfusionNumber);
        ArrayList<Object> listTwo = bloodTestService.getTestInfo(transfusionNumber);
        listOne.addAll(listTwo);
        System.out.println(listOne);
        return JsonResult.success(listOne);
    }

    /**
     * 通过术中输血申请
     *
     * @param transfusionSurgey
     * @return
     */
    @RequestMapping(value = "/review/pass", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Object> submitSurgeyApplication(@RequestBody @Valid TransfusionSurgey transfusionSurgey,
                                                      TransfusionReview transfusionReview) {
        String transfusionNumber = transfusionSurgey.getTransfusionNumber();
        System.out.println(transfusionSurgey.getTransfusionNumber());
        transfusionReview.setState("2");
        transfusionReviewService.updateReview(transfusionNumber, transfusionReview);
        return JsonResult.success();
    }


    /**
     * 分页查询
     * */
    @GetMapping("/normal/{currentPage}/{pageSize}")
    @ResponseBody
    public JsonResult<Object> getPage(@PathVariable int currentPage,@PathVariable int pageSize,TransfusionReview transfusionReview,String startTime,String endTime,String reviewStartTime,String reviewEndTime){
        IPage<TransfusionReview> page = transfusionReviewService.getSelectSurPage(currentPage,pageSize,transfusionReview,startTime,endTime,reviewStartTime,reviewEndTime);
        if (currentPage>page.getPages()){
            page =transfusionReviewService.getSelectSurPage((int)page.getPages(),pageSize,transfusionReview,startTime,endTime,reviewStartTime,reviewEndTime);
        }
        return JsonResult.success(page);
    }
    /**
     * 批量删除
     */
    @RequestMapping("/normal/batchDelete")
    @ResponseBody
    public JsonResult<Object> batchDelete(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        ArrayList<Long> transfusionIds = transfusionSurgeyService.selectId(ids);
        transfusionSurgeyService.removeByIds(transfusionIds);
        boolean success = transfusionReviewService.removeByIds(ids);

        if (success) {

            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }

    // *************************************myq*************************end


    // *************************************lrz*************************start
    /**
     * 用于分页
     *
     * @param currentPage
     * @param pageSize
     * @param
     * @return
     */
    @GetMapping("/normal/lrz/{currentPage}/{pageSize}")
    public Result getPage1(@PathVariable int currentPage, @PathVariable int pageSize,
            TransfusionSurgey transfusionSurgey) {
        IPage<TransfusionSurgey> page = transfusionSurgeyService.getPage1(currentPage, pageSize,
                transfusionSurgey);
        // 如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = transfusionSurgeyService.getPage1((int) page.getPages(), pageSize, transfusionSurgey);
        }
        return Result.success(page);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/normal/batchDelete1")
    @ResponseBody
    public Result batchDelete1(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        boolean success = transfusionSurgeyService.removeByIds(ids);
        Result result = new Result();
        if (success) {
            result.setCode("200");
            result.setMsg("删除成功");
        } else {
            result.setCode("500");
            result.setMsg("删除失败");
        }
        return result;
    }

    // *************************************lrz*************************end
}
